package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.bean.TeamModel;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.presenter.contract.SalesmanContract;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.presenter.SalesmanPresenter;
import com.shd.edanpei.ui.adapter.TeamAdapter;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 团队业务员详情
 */

public class SalesmanActivity extends BaseActivity<SalesmanPresenter>
        implements SalesmanContract.SalesmanIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_orderNumber)
    TextView tvOrderNumber;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.tv_rebate)
    TextView tvRebate;

    private TeamAdapter mAdapter;
    private int userID;
    private LoadImageCtrl loadImageCtrl;
    private int teamID;

    @Override
    protected int setLayout() {
        return R.layout.activity_salesman;
    }

    @Override
    protected SalesmanPresenter bindPresenter() {
        return new SalesmanPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

        loadImageCtrl = new LoadImageCtrl(this);
        headline.setText("业务员详情");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TeamAdapter(this);
        recyclerView.setAdapter(mAdapter);

        Bundle extras = getIntent().getExtras();
        userID = extras.getInt(Constant.USER_ID);
        teamID = extras.getInt(Constant.TEAM_ID);

        mPresenter.getDetails(userID, teamID);
        mPresenter.refresh(userID, teamID);

    }

    @Override
    protected void initListener() {
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore(userID, teamID);
            }
        }, recyclerView);
    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }

    @Override
    public void setDetails(TeamModel data) {
        tvName.setText(data.getUserName());
        tvOrderNumber.setText(data.getOrderNum());
        tvTotal.setText(new StringBuilder().append("¥").append(data.getPerformanceTotal()));
        tvRebate.setText(new StringBuilder().append("返利：¥").append(data.getRebateAmountTotal()));
        if (!TextUtils.isEmpty(data.getAvatarUrl())) {
            loadImageCtrl.getGlide().load(data.getAvatarUrl()).into(ivHead);
        }
    }

    @Override
    public TeamAdapter getAdapter() {
        return mAdapter;
    }
}
